package com.huatai.exchange.utils;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.dal.DBUtils;
import com.faneui.service.Dbconfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Component
public class O32QueryUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public String getFundNames(String fundCodes)throws BusinessException{
        String sql = " select vc_fund_name result from tfundinfo where vc_fund_code  in (" + fundCodes + ") " ;
        return getSqlResult(sql);
    }

    public String getSeatId(String fundCodes, String marketId) throws BusinessException {
        String sql = "  select a.vc_seat_id result from ttradeseat a ,tfundinfo b " +
                " where a.l_fund_id = b.l_fund_id and a.c_trade = '1'" +
                " and b.vc_fund_code in (" + fundCodes + ") and a.c_market_no = '" + marketId + "'  group by a.vc_seat_id  ";
        return getSqlResult(sql);
    }

    public String getJsCode(String fundCodes) throws BusinessException {
        String sql = " select a.vc_sequare_code  result from tfundinfo a where a.vc_fund_code in (" + fundCodes + ") and a.vc_sequare_code != '0' group by a.vc_sequare_code ";
        return getSqlResult(sql);
    }

    public String getStockHolderId(String fundCodes, String marketId) throws BusinessException{
        String sql = "   select a.vc_stockholder_id result  from TENTRUSTSTOCKHOLDER a " +
                " inner join tfundinfo b on b.l_fund_id = a.l_fund_id where b.vc_fund_code in ("+fundCodes+ ") " +

                " and a.c_market_no = '"+marketId+"'  group by a.vc_stockholder_id " ;
        return getSqlResult(sql);
    }

    public String getFundName(String fundCode) throws BusinessException {
        Connection conn = null;
        String user = "trade_zc";
        String r;
        try {
            conn = DBUtils.getConnection(Dbconfig.TRADEDB, user, Dbconfig.getPasswordByUserId(user));
            String sql = "select vc_fund_name from tfundinfo where vc_fund_code = ? ";
            List<Map<String, Object>> result = DBUtils.executeQuery(conn, sql, fundCode);
            if (ListUtils.isEmpty(result)) {
                r = "";
            } else {
                r = (String) result.get(0).get("VC_FUND_NAME");
            }
        } catch (Exception ex) {
            logger.error("", ex);
            r = ex.getMessage();
        } finally {
            DBUtils.closeAll(conn);
        }
        return r;
    }

    private String getSqlResult(String sql ) throws BusinessException{
        Connection conn = null;
        String user = "trade_zc";
        StringBuilder r = new StringBuilder("");
        try {
            conn = DBUtils.getConnection(Dbconfig.TRADEDB, user, Dbconfig.getPasswordByUserId(user));
            List<Map<String, Object>> result = DBUtils.executeQuery(conn, sql);
            if (ListUtils.isEmpty(result)) {
               return "";
            } else {
                for(Map<String, Object> map:result){
                    r.append(",").append(map.get("RESULT")) ;
                }
            }
        } catch (Exception ex) {
            logger.error("", ex);
        } finally {
            DBUtils.closeAll(conn);
        }
        return r.toString().substring(1);
    }

}
